/**
 * @(#)IndexPageController.java, 2010-5-16. 
 */
package com.rsstuan.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.rsstuan.data.TuanGouWebItem;
import com.rsstuan.search.ItemSearcher;
import com.rsstuan.service.ChannelService;
import com.rsstuan.service.ItemService;
import com.rsstuan.storage.data.TuanGouChannelData;
import com.rsstuan.storage.data.TuanGouItemData;
import com.rsstuan.storage.impl.JDOTuanGouItemStorage;
import com.rsstuan.utils.CityUtils;
import com.rsstuan.utils.ItemUtils;
import com.rsstuan.utils.StringUtils;

/**
 *
 * @author guosq
 *
 */
public class IndexPageController extends SpringController{
    
    private static final String LIST_VM = "main/list";
    
    private ItemService itemService = ItemService.getInstance();
    
    public ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) {
        if ("search".equals(request.getParameter("method")))
                return search(request, response);
        return show(request, response);
    }
    public ModelAndView showlist(HttpServletRequest request,
            HttpServletResponse response) {
        JDOTuanGouItemStorage itemStorage = new JDOTuanGouItemStorage();
        String city = null;
        int cityid = 1;
        if (request.getParameter("cityid") != null) {
        	try {
        		cityid = Integer.parseInt(request.getParameter("cityid"));
        		city = CityUtils.getCity(cityid); 
        	} catch (Exception e) { }
        }
        if (city == null)
        	city = CityUtils.DEFAULT_CITY;
        Map<Long, TuanGouChannelData> channelMap = ChannelService.getInstance().getAllChannelsMap();
        TuanGouItemData[] items = itemStorage.getActiveItemsByCity(city);
		List<TuanGouWebItem> webItems = new ArrayList<TuanGouWebItem>();
		for (TuanGouItemData item : items) {
        	if (ItemUtils.validate(item)) {
	            TuanGouWebItem witem = new TuanGouWebItem(item);
	            TuanGouChannelData channel = channelMap.get(item.getChannelId());
	            if (channel != null)
	            	witem.setSiteName(channel.getTitle() + "(" + channel.getCity() + ")");
	            webItems.add(witem);
        	}
        }
		TuanGouWebItem[] witemArray = webItems.toArray(new TuanGouWebItem[0]);
		
		String priceseq = ItemUtils.sortByPrice(witemArray);
		String discountseq = ItemUtils.sortByDiscount(witemArray);
		String buyseq = ItemUtils.sortByOrderNumDesc(witemArray);
		

        //Map<String, Object> map = new HashMap<String, Object>();
        return createModelAndView(request, response, LIST_VM,
                "StringUtils", StringUtils.getInstance(),
                "peoplesequence", buyseq,
                "pricesequence", priceseq,
                "discountsequence", discountseq,
                "items", witemArray,
                "cityid", cityid,
                "city", city);
    }
    
    
    public ModelAndView show(HttpServletRequest request,
            HttpServletResponse response) {
        String city = null;
        int cityid = 1;
        if (request.getParameter("cityid") != null) {
            try {
                cityid = Integer.parseInt(request.getParameter("cityid"));
                city = CityUtils.getCity(cityid); 
            } catch (Exception e) { }
        }
        if (city == null)
            city = CityUtils.DEFAULT_CITY;
        
        TuanGouWebItem[] witems = itemService.getAllAcitveItemsByCity(city);
        String buyseq = "", priceseq = "", discountseq = "";
        if (witems != null && witems.length > 0) {
	        TuanGouWebItem[] witemArray = witems.clone();
	        priceseq = itemService.getOrderSequence(city, "priceseq");
	        if (priceseq == null){
	        	priceseq = ItemUtils.sortByPrice(witemArray);
	        	itemService.setOrderSequence(city, "priceseq", priceseq);
	        }
	        discountseq = itemService.getOrderSequence(city, "discountseq");
	        if (discountseq == null ) {
	        	discountseq = ItemUtils.sortByDiscount(witemArray);
	        	itemService.setOrderSequence(city, "discountseq", discountseq);
	        }
	        buyseq = itemService.getOrderSequence(city, "peopleseq");
	        if (buyseq == null) {
	        	buyseq = ItemUtils.sortByOrderNumDesc(witemArray);
	        	itemService.setOrderSequence(city, "peopleseq", buyseq);
	        }
        }
        

        //Map<String, Object> map = new HashMap<String, Object>();
        return createModelAndView(request, response, LIST_VM,
                "StringUtils", StringUtils.getInstance(),
                "peoplesequence", buyseq,
                "pricesequence", priceseq,
                "discountsequence", discountseq,
                "items", witems,
                "cityid", cityid,
                "city", city);
    }
    
    
    public ModelAndView search(HttpServletRequest request,
            HttpServletResponse response) {
        String key = request.getParameter("q");
        String cityIdStr = request.getParameter("cityid");
        int cityid = 1;
        String city = null;
        if (cityIdStr != null) {
        	try {
        		cityid = Integer.parseInt(request.getParameter("cityid"));
        		city = CityUtils.getCity(cityid); 
        	} catch (Exception e) { }
        }
        if (city == null) {
        	city = CityUtils.DEFAULT_CITY;
        }
        List<TuanGouWebItem> witems = new ArrayList<TuanGouWebItem>();
        if (key != null) {
            witems = ItemSearcher.searchKeyword(key);
        }
        TuanGouWebItem[] witemArrays = witems.toArray(new TuanGouWebItem[0]);
        String priceseq = ItemUtils.sortByPrice(witemArrays);
        String discountseq = ItemUtils.sortByDiscount(witemArrays);
        String buyseq = ItemUtils.sortByOrderNumDesc(witemArrays);
        

        //Map<String, Object> map = new HashMap<String, Object>();
        return createModelAndView(request, response, LIST_VM,
                "StringUtils", StringUtils.getInstance(),
                "peoplesequence", buyseq,
                "pricesequence", priceseq,
                "discountsequence", discountseq,
                "items", witems,
                "cityid", cityid,
                "city", city);
    }
    

}
